package com.zhang.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author zhangshuaiyin
 */
public class StringUtils {

    /**
     * 下划线
     */
    private static final char LINE = '_';
    /**
     * 空字符
     */
    private static final char CHAR_EMPTY = '\0';

    /**
     * 0
     */
    private static final String ZERO = "0";

    public static boolean isEmpty(Object obj) {
        return obj == null || obj.toString().length() == 0;
    }

    public static boolean isBlank(Object obj) {
        return obj == null || obj.toString().trim().length() == 0;
    }

    public static boolean isNotBlank(Object obj) {
        return !isBlank(obj);
    }

    public static Integer getInteger(String s) {
        if (isNotBlank(s)) {
            return Integer.valueOf(s);
        }
        return null;
    }

    public static int getInt(String s) {
        if (isNotBlank(s)) {
            return Integer.parseInt(s);
        }
        return 0;
    }

    private static final Pattern LINE_PATTERN = Pattern.compile("_(\\w)");

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = LINE_PATTERN.matcher(str);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(buffer, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    private static final Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");

    /**
     * 驼峰转下划线
     */
    public static String humpToLine(String str) {
        Matcher matcher = HUMP_PATTERN.matcher(str);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    /**
     * 实现StringBuilder的replaceAll
     *
     * @param stb
     * @param oldStr 被替换的字符串
     * @param newStr 替换oldStr
     * @return
     */
    public static StringBuilder replaceAll(StringBuilder stb, String oldStr, String newStr) {
        if (stb == null || oldStr == null || newStr == null || stb.length() == 0 || oldStr.length() == 0) {
            return stb;
        }
        int index = stb.indexOf(oldStr);
        if (index > -1 && !oldStr.equals(newStr)) {
            int lastIndex = 0;
            while (index > -1) {
                stb.replace(index, index + oldStr.length(), newStr);
                lastIndex = index + newStr.length();
                index = stb.indexOf(oldStr, lastIndex);
            }
        }
        return stb;
    }

    /**
     * 首字母大写
     *
     * @param value
     * @return
     */
    public static String toUpperFirstCode(String value) {
        if (isNotBlank(value)) {
            value = value.substring(0, 1).toUpperCase() + value.substring(1);
        }
        return value;
    }

    /**
     * 字符串转数组
     *
     * @param str      字符串
     * @param splitStr 分隔符
     * @return
     */
    public static String[] stringToArray(String str, String splitStr) {
        String[] arrayStr = null;
        if (isNotBlank(str)) {
            if (str.indexOf(splitStr) != -1) {
                arrayStr = str.split(splitStr);
            } else {
                arrayStr = new String[1];
                arrayStr[0] = str;
            }
        }
        return arrayStr;
    }

    /**
     * 拼接字符串, 默认下划线拼接
     *
     * @param strs
     * @return
     */
    public static String join(String... strs) {
        return join(LINE, strs);
    }

    /**
     * 拼接字符串
     *
     * @param separator
     * @param str
     * @return
     */
    public static String join(char separator, String... str) {
        return org.apache.commons.lang3.StringUtils.join(str, (separator == CHAR_EMPTY ? "" : String.valueOf(separator)));

    }

    /**
     * 不满位数，左补0操作
     *
     * @param str
     * @param length
     * @return
     */
    public static String leftPadZero(String str, int length) {
        if (str == null) {
            return "";
        }

        if (str.length() >= length) {
            return str;
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length - str.length(); i++) {
            buffer.append(ZERO);
        }
        buffer.append(str);
        return buffer.toString();
    }

    /**
     * 不满位数，右补0操作
     *
     * @param str
     * @param length
     * @return
     */
    public static String rightPadZero(String str, int length) {
        if (str == null) {
            return "";
        }

        if (str.length() >= length) {
            return str;
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length - str.length(); i++) {
            buffer.append(ZERO);
        }
        return str.concat(buffer.toString());
    }
}
